<template>
  <div id="app">
    <JsonTree :data="directoryJsonData"
      @selected="cancelEditing"
      @createChildren="onChildrenCreating"
      :editable="!editing"
      style="width: 100%;"
      />
    <div class="edit-cell" v-if="editing">
      <select v-model="editingData.iconType">
        <option v-for="iconType of IconTypeEnum" :key="iconType" :value="iconType">
          {{iconType}}
        </option>
      </select>
      <input type="text" v-model.lazy="editingData.icon" />

      <input type="text" v-model="editingData.title" />
      <button @click="confirmEditing">确定</button>
      <button @click="cancelEditing">取消</button>
      <JsonTreeCell
        :data="editingData"
        :editable="false"
        :selected="true"
      ></JsonTreeCell>
    </div>
  </div>
</template>

<script>
import Vue from 'vue'
import JsonTree from "./components/JsonTree"
import { default as JsonTreeCell, ICON_TYPE as IconTypeEnum, DATA_TYPE as DataObject } from './components/JsonTreeCell'

export default {
  name: 'app',
  data() {
    return {
      editing: false,
      editingData: {
        ...DataObject
      },
      directoryJsonData: {
        title: 'tree root',
        icon: '业',
        iconType: "font",
        children: [
          {
            title: 'tree children',
            icon: 'anchor',
            iconType: "icon",
            children: [
              {
                title: 'abc def',
                icon: 'angry',
                iconType: "icon",
              }
            ]
          },
          {
            title: 'hhh',
            icon: 'ad',
            iconType: "icon",
            children: [],
          },
        ],
      },
    }
  },
  computed: {
    IconTypeEnum() {
      return IconTypeEnum;
    } 
  },
  methods: {
    onChildrenCreating(stack) {
      const vm = this;
      vm.editing = true;
      vm.editingStack = stack;
    },
    confirmEditing() {
      const vm = this;
      let targetTree = vm.editingStack[0];
      let targetData = targetTree.data;
      if (null == targetData.children) {
        Vue.set(targetData, 'children', []);
      }
      targetData.children.push({
        ...vm.editingData
      });
      targetTree.triggerRefreshSize();
      vm.editing = false;
      vm.editingStack = null;
      vm.editingData = {
        ...DataObject
      };
    },
    cancelEditing() {
      const vm = this;
      vm.editing = false;
      vm.editingStack = null;
    },
  },
  components: {
    JsonTree,
    JsonTreeCell
  },
  created() {
    const vm = this;
    vm.editingStack = null;
  }
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: left;
  color: #2c3e50;
  margin-top: 60px;
}
/* cell style */
.edit-cell {
  display: block;
  position: relative;
  margin: 0 0 .2rem 2rem;
  text-align: left;
  height: 1.5rem;
  line-height: 1.5rem;
  padding: 0 .25rem;
  box-sizing: border-box;
}
.edit-cell {
  display: inline-block;
  height: auto;
}

.edit-cell>* {
  vertical-align: middle;
  margin: 0 .1rem;
}
</style>
